package com.user.controller;


import com.user.entity.User;
import com.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpSession;

@Controller
public class LoginControl {

    @Autowired
    private UserService userService;

    @RequestMapping(value = "/login",method = RequestMethod.GET)
    public String login(){
        return "login";
    }
    @RequestMapping(value = "/loginA",method = RequestMethod.POST)
    public String login(HttpSession session, String name, String password) throws Exception{
        User user=userService.login(name,password);
        if(user==null){
            return "redirect:/login";
        }
        else{
            session.setAttribute("username",name);
            if(user.isAdm()){
                System.out.println(user.getName()+" admin login success");
                session.setAttribute("user", name);
                System.out.println("loginA admin:"+name);
                return "redirect:/head";
            }else{
                session.setAttribute("user", name);
                System.out.println("loginA:"+name);
                return "success";
            }
        }
    }

    @RequestMapping(value="/logout")
    public String logout(HttpSession session) throws Exception{
        //清除Session
        //session.invalidate();
        session.removeAttribute("user");
        session.invalidate();
        return "redirect:/login";
    }

}
